const path = require("path");
const express = require("express");
const db = require(path.join(__dirname, "../db"));
const router = express.Router();

// 获取所有的书籍接口
// 获取所有书籍接口
router.get("/getbooks", async(req, res) => {
    let r1 = await db("select * from student");
    let r2 = await db("select count(*) total from student");
    res.send({
        status: 200,
        msg: "获取成功",
        data: r1,
        total: r2[0].total,
    });
});

// 添加书籍接口
router.post("/addbook", async(req, res) => {
    let r1 = await db("insert into student set ?", req.body);
    res.send({
        status: 200,
        msg: "添加成功",
    });
});

// 删除书籍接口
router.get("/delbook", async(req, res) => {
    let id = req.query.id;
    let r1 = await db("delete from student where id=?");
    res.send({
        status: 200,
        msg: "删除成功",
    });
});

module.exports = router;